package com.bawei.a2010douyin.ui.shopping;

import com.bawei.a2010douyin.base.mvp.BasePresenter;
import com.bawei.a2010douyin.base.rx.BaseFunc;
import com.bawei.a2010douyin.entity.GoodsEntity;

import java.util.List;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ShoppingPresenter extends BasePresenter<ShoppingContract.IShoppingView> implements ShoppingContract.IShoppingPresenter {
    private ShoppingContract.IShoppingModel mIShoppingModel;
    public ShoppingPresenter() {
        mIShoppingModel = new ShoppingModel();
    }

    @Override
    public void goods(int category_id, int currentPage, int pageSize) {
        mIShoppingModel.goods(category_id, currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new BaseFunc<List<GoodsEntity>>())
                .subscribe(new Observer<List<GoodsEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mCompositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(List<GoodsEntity> goodsEntities) {
                        if(isAttach()){
                            getView().showGoods(goodsEntities);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if(isAttach()){
                            getView().onError();
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
